// created by noellynch
// Mar 26, 2008

package com.apachepivot.utils;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;

import javax.swing.JComponent;

public final	class GraphicsUtils 
{
	/**
	 * What is says on the tin. Draws a line between 2 points.
	 * @param cl_g
	 * @param cl_ptA
	 * @param cl_ptB
	 */
	final	static	public	void	drawLine(Graphics cl_g, Point cl_ptA, Point cl_ptB) {
		cl_g.drawLine(cl_ptA.x, cl_ptA.y, cl_ptB.x, cl_ptB.y);
	}
	
	/**
	 * Draw a line wth a start point.
	 * @param cl_g
	 * @param cl_ptA
	 * @param cl_ptB
	 */
	final	static	public	void	drawLineAsVector(Graphics cl_g, Point cl_ptA, Point cl_ptB) {
		cl_g.drawLine(cl_ptA.x, cl_ptA.y, cl_ptB.x, cl_ptB.y);
		cl_g.drawOval(cl_ptA.x - 2, cl_ptA.y - 2, 4, 4);
		cl_g.fillOval(cl_ptB.x - 2, cl_ptB.y - 2, 4, 4);
	}
	
	/**
	 * Finds middle of a rectangle.
	 * @param cl_r
	 * @return
	 */
	final	static	public	Point	centreOfRect(Rectangle cl_r) {
		return new Point(cl_r.x + (cl_r.width >> 1), cl_r.y + (cl_r.height >> 1));
	}
	
	/**
	 * Fills a JComponent rect.
	 * @param cl_g
	 * @param cl_comp
	 */
	final	static	public	void	fillComponentRect(Graphics cl_g, JComponent cl_comp) {
		cl_g.fillRect(0, 0, cl_comp.getWidth(), cl_comp.getHeight());
	}
	
	/**
	 * Draws a JComponent rect.
	 * @param cl_g
	 * @param cl_comp
	 */
	final	static	public	void	drawComponentRect(Graphics cl_g, JComponent cl_comp) {
		cl_g.drawRect(0, 0, cl_comp.getWidth() - 1, cl_comp.getHeight() - 1);
	}
	
	/**
	 * Fills a JComponent round rect.
	 * @param cl_g
	 * @param cl_comp
	 */
	final	static	public	void	fillComponentRoundRect(Graphics cl_g, JComponent cl_comp) {
		cl_g.fillRoundRect(0, 0, cl_comp.getWidth() - 1, cl_comp.getHeight() - 1, 10, 10);
	}
	
	/**
	 * Draws a JComponent round rect.
	 * @param cl_g
	 * @param cl_comp
	 */
	final	static	public	void	drawComponentRoundRect(Graphics cl_g, JComponent cl_comp) {
		cl_g.drawRoundRect(0, 0, cl_comp.getWidth() - 1, cl_comp.getHeight() -1, 10, 10);
	}
	
	/**
	 * Draw a points as a rect.
	 * @param cl_g
	 * @param cl_pt
	 * @param i_dim
	 */
	final	static	public	void	drawPointsAsRect(Graphics cl_g, Point cl_a, Point cl_b) {
		cl_g.drawLine(cl_a.x, cl_a.y, cl_a.x, cl_b.y);
		cl_g.drawLine(cl_a.x, cl_b.y, cl_b.x, cl_b.y);
		cl_g.drawLine(cl_b.x, cl_b.y, cl_b.x, cl_a.y);
		cl_g.drawLine(cl_b.x, cl_a.y, cl_a.x, cl_a.y);
	}
	
	/**
	 * Draw a point as a rect.
	 * @param cl_g
	 * @param cl_pt
	 * @param i_dim
	 */
	final	static	public	void	drawPoint(Graphics cl_g, Point cl_pt, int i_dim) {
		int		li_dim = i_dim >> 1;
		cl_g.drawRect(cl_pt.x - li_dim, cl_pt.y - li_dim, i_dim, i_dim);
	}
	
	final	static	public	void	drawPoint(Graphics cl_g, Point2D cl_pt, int i_dim) {
		int		li_dim = i_dim >> 1;
		cl_g.drawRect((int)cl_pt.getX() - li_dim, (int)cl_pt.getY() - li_dim, i_dim, i_dim);
	}
	
	/**
	 * Fill a point as a rect.
	 * @param cl_g
	 * @param cl_pt
	 * @param i_dim
	 */
	final	static	public	void	fillPoint(Graphics cl_g, Point cl_pt, int i_dim) {
		int		li_dim = i_dim >> 1;
		cl_g.fillRect(cl_pt.x - li_dim, cl_pt.y - li_dim, i_dim, i_dim);
	}
	
	final	static	public	void	fillPoints(Graphics cl_g, Point cl_a, Point cl_b) {
		Point		lcl_a = new Point();
		Point		lcl_b = new Point();
		
		if(cl_a.x < cl_b.x) {
			lcl_a.x = cl_a.x;
			lcl_b.x = cl_b.x;
		} else {
			lcl_b.x = cl_a.x;
			lcl_a.x = cl_b.x;
		}
		
		if(cl_a.y < cl_b.y) {
			lcl_a.y = cl_a.y;
			lcl_b.y = cl_b.y;
		} else {
			lcl_b.y = cl_a.y;
			lcl_a.y = cl_b.y;
		}
		
		cl_g.fillRect(lcl_a.x, lcl_a.y, lcl_b.x - lcl_a.x, lcl_b.y - lcl_b.y);
	}
	
	final	static	public	void	fillPoints(Graphics2D cl_g, Point cl_a, Point cl_b) {
		Point		lcl_a = new Point();
		Point		lcl_b = new Point();
		
		if(cl_a.x < cl_b.x) {
			lcl_a.x = cl_a.x;
			lcl_b.x = cl_b.x;
		} else {
			lcl_b.x = cl_a.x;
			lcl_a.x = cl_b.x;
		}
		
		if(cl_a.y < cl_b.y) {
			lcl_a.y = cl_a.y;
			lcl_b.y = cl_b.y;
		} else {
			lcl_b.y = cl_a.y;
			lcl_a.y = cl_b.y;
		}
		
		cl_g.fillRect(lcl_a.x, lcl_a.y, lcl_b.x - lcl_a.x, lcl_b.y - lcl_a.y);
	}
	
	/**
	 * Draw an oval in a rectangle.
	 * @param cl_g
	 * @param cl_r
	 */
	final	static	public	void	drawOval(Graphics cl_g, Rectangle cl_r) {
		cl_g.drawOval(cl_r.x, cl_r.y, cl_r.width, cl_r.height);
	}
	
	/**
	 * Sets an alpha.
	 * @param cl_g
	 * @param f_a
	 */
	final	static	public	void	alpha(Graphics2D cl_g, float f_a) {
		cl_g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, f_a));
	}
	
	/**
	 * Turns on anti-aliasing.
	 * @param cl_g
	 */
	final	static	public	void	antiAliasOn(Graphics2D cl_g) {
		cl_g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	}
	
	/**
	 * Sets up stroke.
	 * @param cl_g
	 * @param f_stroke
	 */
	final	static	public	void	stroke(Graphics2D cl_g, float f_stroke) {
		cl_g.setStroke(new BasicStroke(f_stroke));
	}
	
	final	static	public	int		getStringWidth(Graphics2D cl_g, String str_s) {
		return (int)cl_g.getFontMetrics().getStringBounds(str_s, cl_g).getWidth();
	}
	
	final	static	public	int		getStringHeight(Graphics2D cl_g, String str_s) {
		return (int)cl_g.getFontMetrics().getStringBounds(str_s, cl_g).getHeight();
	}
}
